<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\GrpShopPosition;

/**
 * ShopPositionSearch represents the model behind the search form about `backend\models\GrpShopPosition`.
 */
class ShopPositionSearch extends GrpShopPosition
{
    /**
     * @inheritdoc
     */
	public $brand_name;
    public function rules()
    {
        return [
            [['id', 'brand_id'], 'integer'],
            [['sales_name', 'province', 'city', 'address', 'tel','brand_name'], 'safe'],
            [['longitude', 'latitude'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = GrpShopPosition::find()->joinWith(['brandName']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'brand_id' => $this->brand_id,
            'longitude' => $this->longitude,
            'latitude' => $this->latitude,
        ]);

        $query->andFilterWhere(['like', 'sales_name', $this->sales_name])
            ->andFilterWhere(['like', 'province', $this->province])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'tel', $this->tel])
            ->andFilterWhere(['in', 'brand_id', $this->brand_id])
            ->andFilterWhere(['like', 'grp_brand.name', $this->brand_name])
        ;

        return $dataProvider;
    }
}
